/*
        Copyright (C) 2007  Aaron Defazio

        This program is free software; you can redistribute it and/or
        modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation; either version 2
        of the License, or (at your option) any later version.

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program; if not, write to the Free Software
        Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;
using CodeProject;
using CodeProject.Win32;

namespace BlinkLn
{
    //Adapted from the codeproject code
    //Handles the unmanaged api requrired to access the windows hotkey stuff
    class HotKey : IDisposable
    {
        public event System.EventHandler Pressed;
        protected DummyWindowWithEvent window = new DummyWindowWithEvent();	//window for WM_Hotkey Messages
        private bool IsRegistered;

        #region IDisposable Members

        public void Dispose()
        {
            if (IsRegistered)
            {
                CodeProject.Win32.User32.UnregisterHotKey(window.Handle, this.GetType().GetHashCode());
            }
        }

        #endregion

        public HotKey()
        {
            window.ProcessMessage += new MessageEventHandler(window_ProcessMessage);
        }

        void window_ProcessMessage(object Sender, ref Message msg, ref bool Handled)
        {
            if ( (msg.Msg == (int)CodeProject.Win32.Msgs.WM_HOTKEY) && (msg.WParam == (IntPtr)this.GetType().GetHashCode()))
            {
                Handled = true;
                if (Pressed != null)
                    Pressed(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// Default version creates the alt-space hotkey
        /// </summary>
        public void RegisterHotKey()
        {
            int mod = (int)CodeProject.Win32.Modifiers.MOD_ALT;
            int SpaceKey = 32;
            CodeProject.Win32.User32.RegisterHotKey(window.Handle, this.GetType().GetHashCode(), mod, SpaceKey );
        }
        public void RegisterHotKey(Keys[] Modifiers, int keynum)
        {
            int mod = 0;
            //(int)Win32.Modifiers.MOD_ALT;
            foreach (Keys Modifier in Modifiers)
            {
                if (Modifier == Keys.Alt)
                    mod += (int)CodeProject.Win32.Modifiers.MOD_ALT;
                if (Modifier == Keys.Control)
                    mod += (int)CodeProject.Win32.Modifiers.MOD_CONTROL;
                if (Modifier == Keys.Shift)
                    mod += (int)CodeProject.Win32.Modifiers.MOD_SHIFT;
            }

            IsRegistered = true;
            CodeProject.Win32.User32.RegisterHotKey(window.Handle, this.GetType().GetHashCode(), mod, keynum);
        }
    }
}
